Back to Editorials CodeCraft β
  • Github
  • Instagram
< >

Summation of Primes

TIME LIMIT = 1 SEC.

  • Okay, enough stories, don't you think? Let's have a straightforward problem:
    Given a number N, find the sum of all primes strictly below N.
Input Output
First line will contain N. Print the sum of all primes strictly below N.
Constraints
  • 1 ≤ N ≤ 106
Example Test Case
Input             Output
10 17
Solution

#include <iostream> using namespace std; int main() { unsigned long long int n,i,sum=0; cin>>n; bool Sieve[n]; for(i=0;i<n;i++) Sieve[i]=true; Sieve[0]=false; Sieve[1]=false; // Check all nos from 2 to 1 million for(long i = 2; i < n; ++i) { if( Sieve[i]==false ) // If marked not prime continue; // return to head of loop else // Set all multiples as not prime for(long j = 2*i; j < n; j += i) Sieve[j] = false; } for(long i = 2; i < n; ++i) if( Sieve[i]==true ) // Add all nos with bit set sum += i; cout << sum << endl; return 0; }
  • #1 Thieves
  • #2 The Queue of Doubts
  • #3 Summation of Primes
  • #4 Spacious Maximus
  • #5 Samosas
  • #6 Reasonably Sound
  • #7 Product of Digits
  • #8 Prime Usernames
  • #9 Path Shifter
  • #10 Keypad Count
  • #11 Even Vowels
  • #12 Encryption
  • #13 Decryption
  • #14 Canteen Accountant
  • #15 Attendance Register
  • #16 Amazing Year

Get in touch

  • executives@codingstudio.club
  • +91 9010342360
  • Vignana Bharthi Instute of Technology
    Aushapur, Ghatkesar, Hyderabad, Telengana - India

© coding.Studio();